﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using GMS.Dinasty.Reporting;
using DevExpress.XtraReports.UI;


namespace GMS.BasicControls
{
    public partial class ChooseDistributor : BackGroundControl
    {
        public ChooseDistributor(string connectionString)
        {
            InitializeComponent();
            dataGridViewDistributors.DataSource = _distributorBSource;
            dataGridViewDistributors.AutoGenerateColumns = false;



            dataGridViewContractors.DataSource = _userBSource;
            dataGridViewContractors.AutoGenerateColumns = false;

            _connectionString = connectionString;
        }

        SqlDataAdapter _da = new SqlDataAdapter();
        BindingSource _distributorBSource = new BindingSource();
        BindingSource _userBSource = new BindingSource();

        private DataSet _ds = new DataSet();

        private string _connectionString;

        private DataTable _data;
        public DataTable Data
        {
            get { return _data; }
            set { _data = value; }
        }
        private string _specificationNumber;
        public string SpecificationNumber
        {
            get { return _specificationNumber; }
            set { _specificationNumber = value; }
        }
        private string _distributor;
        public string Distributor
        {
            get { return _distributor; }
            set { _distributor = value; }
        }
        private string _contractor;
        public string Contractor
        {
            get { return _contractor; }
            set { _contractor = value; }
        }
        private int _idContractor;
        private int _idDistributor;
        
        private void ChooseDistributor_Load(object sender, EventArgs e)
        {
            SqlConnection connection = new SqlConnection(_connectionString);
            connection.Open();
            SqlDataAdapter da = new SqlDataAdapter();
            da.SelectCommand = new SqlCommand("select * from DDistributor");
            da.SelectCommand.Connection = connection;
            da.Fill(_ds, "distributor");

            da = new SqlDataAdapter();
            da.SelectCommand = new SqlCommand("select * from [Distributors_Users] DU, Users U WHERE 	DU.IdUser = U.IdUser");
            da.SelectCommand.Connection = connection;
            da.Fill(_ds, "distributor_user");

            CreateRelations();

            _distributorBSource.DataSource = _ds;
            _distributorBSource.DataMember = "distributor";


            _userBSource.DataSource = _distributorBSource;
            _userBSource.DataMember = "first";
        }
        private void CreateRelations()
        {
            DataColumn _parentColumn = _ds.Tables["distributor"].Columns["id"];
            DataColumn _childColumn = _ds.Tables["distributor_user"].Columns["IdDistributor"];
            DataRelation _relation = new DataRelation("first", _parentColumn, _childColumn);

            _ds.Relations.Add(_relation);
        }

        private void applylinkLabel_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            SqlConnection connection = new SqlConnection(_connectionString);
            //_distributor = dataGridViewDistributors.CurrentRow.Cells["Caption"].Value.ToString();
            _contractor = dataGridViewContractors.CurrentRow.Cells["Column3"].Value.ToString() + " " + dataGridViewContractors.CurrentRow.Cells["Column4"].Value.ToString().Substring(0,1) + ". " + dataGridViewContractors.CurrentRow.Cells["Column5"].Value.ToString().Substring(0,1) + ".";
            _idContractor = int.Parse(dataGridViewContractors.CurrentRow.Cells["ColumnIdContractor"].Value.ToString());
            _idDistributor = int.Parse(dataGridViewDistributors.CurrentRow.Cells["IdDistributor"].Value.ToString());

            closeLabel_LinkClicked(sender, e);
            orderReport rep = new orderReport(_specificationNumber, _contractor, _data);
            rep.ShowPreview();

            SqlCommand _insertApplication = new SqlCommand();
            _insertApplication.Connection = connection;
            _insertApplication.CommandText = "DInsertApplication "+int.Parse(_specificationNumber)+", "+_idContractor+", "+_idDistributor;


            connection.Open();
            string s = _insertApplication.ExecuteScalar().ToString();

            int _idApplications = int.Parse(s);

            _da.InsertCommand = new SqlCommand("DinsertApplicationsContent");
            _da.InsertCommand.CommandType = CommandType.StoredProcedure;
            _da.InsertCommand.Connection = connection;

            SqlParameter _insertParameterContent = null;
            _insertParameterContent = _da.InsertCommand.Parameters.Add("@OrderContent", SqlDbType.Int);
            _insertParameterContent.SourceColumn = "order";
            _insertParameterContent.SourceVersion = DataRowVersion.Current;

            _insertParameterContent = _da.InsertCommand.Parameters.Add("@IdApplication", SqlDbType.Int);
            _insertParameterContent.Value = _idApplications;
            

            _insertParameterContent = _da.InsertCommand.Parameters.Add("@IdGood", SqlDbType.Int);
            _insertParameterContent.SourceColumn = "idgood";
            _insertParameterContent.SourceVersion = DataRowVersion.Current;

            _insertParameterContent = _da.InsertCommand.Parameters.Add("@CaptionGood", SqlDbType.Text);
            _insertParameterContent.SourceColumn = "caption";
            _insertParameterContent.SourceVersion = DataRowVersion.Current;

            _insertParameterContent = _da.InsertCommand.Parameters.Add("@Unit", SqlDbType.Text);
            _insertParameterContent.SourceColumn = "units";
            _insertParameterContent.SourceVersion = DataRowVersion.Current;

            _insertParameterContent = _da.InsertCommand.Parameters.Add("@Quantity", SqlDbType.Float);
            _insertParameterContent.SourceColumn = "quantity";
            _insertParameterContent.SourceVersion = DataRowVersion.Current;

            _da.Update(_data);
            _data.AcceptChanges();
        }

    }
}
